﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace jsonedit
{
    public partial class CfgEdit : Form
    {
        public CfgEdit()
        {
            InitializeComponent();
        }
        Dictionary<string, string> names = new Dictionary<string, string>() {
          { "cfg","设置"},
           { "len64","不显示"},
        {"structPP","选项" },
        { "devi","设备"},
        { "discription","名称"},
        { "unused","不显示"},
        { "id","编号ID"},
        { "cfg_index","不显示"},
        { "chksum","不显示"},
        { "bitCalibrationEnabled","时间自校"},
        { "bSW","开启"},//多个
        { "bPWR_NON","设备常开"},
        { "bTTL_CONNECT","TTL连接"},
        { "gi","配置"},
        { "port[","端口[]"},
        { "bREC","记录"},
        { "iPort","端口号"},
        { "iProtocol","协议号"},
        { "min_gap","最小通信间隔"},
        { "beforeTx","前置通信时延"},
        { "afterTx","后置通信时延"},
        { "iType","通信类型"},
        { "baudrate","波特率"},
        { "para","参数"},
        { "prjs[","任务[]"},
        { "type","类型"},
        { "interval","间隔"},
        { "Interval","时间间隔"},

        { "delay","延时"},
        { "UNIT","单位"},
        { "delay_random","随机"},
        { "group","要素组"},
        { "cx","要素号"},
        { "iFmtBelt","信息格式号"},
        { "iComCfg","通信配置号"},
        { "elsy","系统要素"},
        { "elem[","要素[]"},
        { "Name","名称"},
        { "Fmt","显示格式"},
        { "Bits","有效BITS"},
        { "Offset","时间偏置"},
        { "wayMODIFY","修正"},
        { "k","修正K"},
        { "b","修正B"},
        { "elem","一般要素"},
        { "eldt","时基要素"},
        { "bcfg[","信息格式[]"},
         { "cmfg[","通信配置[]"}




    };
        //修改
        private void button1_Click(object sender, EventArgs e)
        {
            DllTest.DLL mdll = new DllTest.DLL();
            txt_templet.Text = mdll.doWriteDevice(label1.Text,txt_val.Text, txt_templet.Text, txt_sMdl.Text);
        }
        //变更事件
        private void textBox4_TextChanged(object sender, EventArgs e)
        {
            DllTest.DLL mdll = new DllTest.DLL();
     
          
            string cfg = mdll.doReadDevice(txt_fmtOut.Text, txt_templet.Text, txt_sMdl.Text);
            DataEntity.CFGEntity cfgModel = new DataEntity.CFGEntity();
            cfgModel = (DataEntity.CFGEntity)JsonConvert.DeserializeObject(cfg, cfgModel.GetType());
            DataEntity.CFGView view = new DataEntity.CFGView();
            view.name = "cfg";

            if (cfgModel != null)
            {


                foreach (DataEntity.CFGItem itemModel in cfgModel.CFG)
                {
                    DataEntity.CFGView _view = view;
                    string name = itemModel.name.Remove(0, itemModel.name.LastIndexOf('.') + 1);
                    foreach (string item in itemModel.name.Split('.'))
                    {
                        if (item != "")
                        {
                            if (item != name)
                            {
                                if (_view.name != item)
                                {
                                    DataEntity.CFGView _view1 = _view.children.Find(a => a.name == item);
                                    if (_view1 == null)
                                    {
                                        _view1 = new DataEntity.CFGView() { name = item };
                                        _view.children.Add(_view1);
                                    }
                                    _view = _view1;
                                }

                            }
                            else
                            {
                                DataEntity.CFGView _view1 = new DataEntity.CFGView() { name = item, model = itemModel };
                                _view.children.Add(_view1);
                            }
                        }
                    }
                }

            }
            //解析到DataTable数据集
     


            TreeNode tn = new TreeNode();
            tn.Name = "设置";
            tn.Text = "设置";
            tn.Expand();
            addNodes(ref tn, view.children);


            while (treeView1.Nodes.Count > 0)
            {
                treeView1.Nodes.RemoveAt(0);
            }
            treeView1.Nodes.Add(tn);
            //treeView1.e.ExpandAll();
        }
        public void addNodes(ref TreeNode tn, List<DataEntity.CFGView> childrens)
        {
            foreach (DataEntity.CFGView item in childrens)
            {
                TreeNode node = new TreeNode();
                node.Text =getname(item.name);
                node.Name = "";
                if (item.model != null)
                {
                    node.Name = item.model.name;

                    node.ToolTipText = item.model.value ;

                }
                
                                    addNodes(ref node, item.children);
                tn.Nodes.Add(node);
            }
        }
        /// <summary>
        /// 添加子节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                if (treeView1.SelectedNode.Name != "")
                {
                    panel1.Enabled = true;
                    txt_val.Text = treeView1.SelectedNode.ToolTipText;
                    label1.Text = treeView1.SelectedNode.Name;
                }
                else
                {
                    panel1.Enabled = false;
                }
            }

        }
        /// <summary>
        /// 获取别名
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public string getname(string name)
        {
            if (name.Contains("["))
            {
                if (names.ContainsKey(name.Split('[')[0] + "["))
                {
                    name = names[name.Split('[')[0] + "["].Replace("[]", name.Split('[')[1].Replace("]", ""));
                }
            }
            else if (names.ContainsKey(name))
            {
                name = names[name];
            }
            return name;
        }
    }
}
